<?php
function cutStringuser($str,$limit) {
    if(strlen($str)<=$limit) {
        return $str;
    }else {
        $arrStr = explode(" ",$str);
        if(count($arrStr)==1) {
            return strip_tags(substr($str, 0, 20));
        }

        if(count($arrStr)>=3) {
            $newLimit = $arrStr[0] . " " . $arrStr[1] . " " . $arrStr[2];
            $n=2;
        }else if(count($arrStr)==2) {
            $newLimit = $arrStr[0] . " " . $arrStr[1];
            $n = 1;
        }
        if(strlen($newLimit)<=$limit-2) {
            $newStr = $newLimit . "..";
            return $newStr;
        }else {
            $newLimit = $arrStr[0];
            for($i=1; $i<$n; $i++) {
                $newLimit = $newLimit . " " . $arrStr[$i];
            }
            if(strlen($newLimit)<=$limit-2) {
                $newStr = $newLimit . "..";
                return $newStr;
            }else {
                $newStr = $arrStr[0] . "..";
                if(strlen($newStr)<= $limit-2) {
                    return $newStr;
                }else {
                    return strip_tags(substr($newStr, 0, 20));
                }
            }
        }
    }
}
?>

<div id="core_menu_mini_menu">
    <?php
    // Reverse the navigation order (they're floating right)
    $count = count($this->navigation);
    foreach( $this->navigation->getPages() as $item ) $item->setOrder(--$count);
    // echo "<span style='color: red'>" . $this->viewer()->getIdentity() . "</span>";
    $route = array('route'=>'user_logout', 'action'=>'logout');
    $viewer = $this->viewer();
    ?>
       
    <?php
    if( $viewer && $viewer->getIdentity() )
        {
    ?>      <div class="display_member_home" >
                <div class="logo_member_home">
                    <a href="<?php echo $this->baseUrl(); ?>"><img src="<?php echo $this->baseUrl('/application/themes/talktangle/images/logo_hp.png') ?>" id="logo"></a> 
                    <span><?php echo $this->translate('The socail media engagemet network!') ?></span>
                </div>
                <div class="fixie">
                    <div class="member_home_content">
                    
                    <div class="menu_mini_member_home">
                        <ul>
                            <?php foreach( $this->navigation as $item ): ?>  
                            <?php
                                if($item->getLabel() == 'Settings') 
                                { 
                            ?>
                            <li id="account_menu_mini" class="view_more_account_mini" onmouseover="Over()" onmouseout="Out()">
                                <a  href="javascript: void(0)" >
                                                <?php echo $this->translate('My Accounts');?>
                                </a>
                                <div id="dropmenu_mini" class="dropmenu_mini">
                                    <div class="user_account_info">
                                        <div class="user_photo">
                                            <?php echo $this->htmlLink($this->viewer()->getHref(), $this->itemPhoto($this->viewer(), 'thumb.profile')) ?>
                                        </div>
                                        <div class="username_editprof">
                                            <h4>
                                                <?php echo $this->translate($this->viewer()->getTitle()); ?>
                                            </h4>
                                        </div>
                                    </div>
                                    <div class="account_option">
                                        <?php echo $this->htmlLink(array(
                                            'module'=>'user',
                                            'controller'=>'settings',
                                            'action'=>'general',
                                            'route'=>'user_extended'
                                        ), $this->translate("General")); ?>
                                        <?php echo $this->htmlLink(array(
                                            'module'=>'user',
                                            'controller'=>'settings',
                                            'action'=>'privacy',
                                            'route'=>'user_extended'
                                        ), $this->translate("Privacy")); ?>
                                        <?php echo $this->htmlLink(array(
                                            'module'=>'user',
                                            'controller'=>'settings',
                                            'action'=>'notifications',
                                            'route'=>'user_extended'
                                        ), $this->translate("Notifications")); ?>                            
                                        <?php echo $this->htmlLink(array(
                                            'module'=>'user',
                                            'controller'=>'settings',
                                            'action'=>'network',
                                            'route'=>'user_extended'
                                        ), $this->translate("Networks")); ?>
                                        <?php echo $this->htmlLink(array(
                                            'module'=>'user',
                                            'controller'=>'settings',
                                            'action'=>'password',
                                            'route'=>'user_extended'
                                        ), $this->translate("Change Password")); ?>
                                        <?php echo $this->htmlLink(array(
                                            
                                            'action'=>'logout',
                                            'route'=>'user_logout'
                                        ), $this->translate("Sign Out")); ?>
                                       
                                    </div>
                                </div>
                            </li>
                                        <?php }       
                                       
                                    else { ?>
                            <li>
                                            <?php
                                                if($item->getLabel() != 'Sign Out'){
                                                    echo $this->htmlLink($item->getHref(), $this->translate($item->getLabel()));
                                                }
                                            ?>
                            </li>
                                        <?php   }
                                    ?>
                                <?php endforeach; ?>
                        </ul>
                    </div>
                    
                    <ul class="jewelContainer">
                        <li class="updates" id="pulldown">
                            <span onclick="toggleUpdatesPulldown(event, this, '4');" class="updates_pulldown">
                                    <!--<?php
                                   
                                    ?>
                                    <?php if( $this->notificationCount ):?>
                                        <span class="counter" id="count_noti"><?php echo $this->notificationCount; ?>Updates</span>
                                    <?php endif;
                                    ?>-->
                                <span style="display: none;" id="updates_toggle"></span>

                                <div class="pulldown_contents_wrapper">
                                    <div class="pulldown_contents">
                                        <ul class="notifications_menu" id="notifications_menu">
                                            <div class="notifications_loading" id="notifications_loading">
                                                <img src='application/modules/Core/externals/images/loading.gif'/>
                                                    <?php echo $this->translate("Loading ...") ?>
                                            </div>
                                        </ul>
                                    </div>
                                    <div class="pulldown_options">
                                            <?php echo $this->htmlLink(array('route' => 'default', 'module' => 'activity', 'controller' => 'notifications'),
                                               $this->translate('View All Updates'),
                                               array('id' => 'notifications_viewall_link')) ?>
                                            <?php echo $this->htmlLink('javascript:void(0);', $this->translate('Mark All Read'), array(
                                              'id' => 'notifications_markread_link',
                                              'onclick' => '$("count_noti").destroy();document.getElementById("updates_toggle").innerHTML = "0"; en4.activity.hideNotifications("' . $this->translate("%s Updates", "0") . '")' 
                                            )) ?>
                                            
                                    </div>   
                                </div>   
                                <a href="javascript:void(0);" id="updates_toggle" <?php if( $this->notificationCount ):?> class="new_updates"<?php endif;?>><?php echo $this->translate(array('%s Update', '%s Updates', $this->notificationCount), $this->locale()->toNumber($this->notificationCount)) ?></a>
                            </span>
                        </li>
                             <script language="javascript" type="text/javascript">
                            function search_focus()
                            {
                                var textbox=document.getElementById('query');

                                if (textbox.value == '<?php echo $this->translate("Search for promotion"); ?>')
                                {
                                    textbox.value='';
                                    textbox.style.color='#000000';
                                }
                            }
                            function search_blur()
                            {
                                var textbox=document.getElementById('query');
                                if (textbox.value=="")
                                {
                                    textbox.value='<?php echo $this->translate("Search for promotion"); ?>';
                                    textbox.style.color="#cccccc";
                                }
                            }
                        </script>

                        <li class ="face_search_form">
                            <form id="global_search_form" action="<?php echo $this->url(array('type'=>'searchpromotion'), 'talktangle_extended', true) ?>" method="post">
                                <div class="text_form_search">
                                    <input class="text_search" id="query"  type="text" alt="" value="<?php echo $this->translate('Search for promotion'); ?>" size="20" onblur="search_blur()" onfocus="search_focus()" name="search"/>
                                    <input class="button_search" id="button_search" name="button_search" type="submit" value="<?php echo $this->translate('Search'); ?>"/>
                                </div>
                            </form>
                        </li>
                    </ul>
                    
                    
                    </div>
                </div>
            </div>    
    <?php
    }
    ?>
    
    <?php
    if(!$viewer || !$viewer->getIdentity() ) 
        {
    ?>      <div class="display_home">
                <div class="logo">
                        <?php echo $this->htmlLink(array('route'=>'default'), "<img src='" . $this->baseUrl('/application/themes/talktangle/images/logo.png') . "' height=110px>",array(tabindex=>'1'))?>
                </div>    
                    <!--<script language="javascript" type="text/javascript">	            
                               function toggleMask(el, maskId) {
                                   var mask = document.getElementById(maskId);
                                   var elVal = el.value;
                                   if (mask) {
                                       if (mask.style.display != 'none') {
                                           mask.style.display = 'none';
                                       }
                                       else {
                                           if (elVal.length == 0) {
                                               mask.style.display = '';
                                           }
                                       }
                                   }
                               }

                            </script>-->   
                <div class="menu_mini" >
                    <form  action="<?php echo $this->url(array('controller' => 'login'), 'default', true)?>" method="POST" >
                        <div class="dv_text_login">
                    <!--<div class="mark_login" style="position: absolute;" id="emailMask"><?php echo $this->translate('Email Address'); ?></div>-->
                            <div class="label"><?php echo $this->translate('Email'); ?> </div>
                            <input class="text_login" type="text" name="email" id="email" value="" tabindex="1" onblur="toggleMask(this, 'emailMask');" onfocus="toggleMask(this, 'emailMask');"/>
                        </div>

                        <div class="dv_text_login">
                    <!--<div class="mark_login" style="position: absolute;" id="pwdMask"><?php echo $this->translate('Password'); ?></div> -->
                            <div class="label"><?php echo $this->translate('Password'); ?> </div>
                            <input class="text_login" type="password" id="password" name="password" value='' tabindex="1" onblur="toggleMask(this, 'pwdMask');" onfocus="toggleMask(this, 'pwdMask');"/>
                        </div> 
                        <div class="button_login"> <button type="submit" tabindex="1" name="submit" id="submit"><?php echo $this->translate('Login');?></button></div>
                        <div class="under_login">
                            <div class="remember">                          
                                <input id="remember" type="checkbox" name="remember" value="1" tabindex="1">
                                <label for="remember"><?php echo $this->translate("Keep me logged in"); ?></label>
                            </div>
                            <div class="forgot_pass">
                                <a tabindex="1" href="<?php echo $this->url(array('module'=>'user','controller' => 'auth','action'=>'forgot'), 'default', true) ;?>"><?php echo $this->translate('Forgot your password?');?></a>
                            </div>
                        </div>
                    </form> 
                </div>
             </div>   
    <?php 
        }
    ?>
</div>      


<script type='text/javascript'>
    var notificationUpdater;

    en4.core.runonce.add(function(){
        new OverText($('global_search_field'), {
            poll: true,
            pollInterval: 500,
            positionOptions: {
                position: ( en4.orientation == 'rtl' ? 'upperRight' : 'upperLeft' ),
                edge: ( en4.orientation == 'rtl' ? 'upperRight' : 'upperLeft' ),
                offset: {
                    x: ( en4.orientation == 'rtl' ? -4 : 4 ),
                    y: 2
                }
            }
        });

        if($('notifications_markread_link')){
            $('notifications_markread_link').addEvent('click', function() {
                //$('notifications_markread').setStyle('display', 'none');
                en4.activity.hideNotifications('<?php echo $this->string()->escapeJavascript($this->translate("0 Updates"));?>');
            });
        }

<?php if ($this->updateSettings && $this->viewer->getIdentity()): ?>
        notificationUpdater = new NotificationUpdateHandler({
            'delay' : <?php echo $this->updateSettings;?>
        });
        notificationUpdater.start();
        window._notificationUpdater = notificationUpdater;
<?php endif;?>
    });


    var toggleUpdatesPulldown = function(event, element, user_id) {
        if( element.className=='updates_pulldown' ) {
            element.className= 'updates_pulldown_active';
            showNotifications();
        } else {
            element.className='updates_pulldown';
        }
    }

    var showNotifications = function() {
        en4.activity.updateNotifications();
        new Request.HTML({
            'url' : en4.core.baseUrl + 'activity/notifications/pulldown',
            'data' : {
                'format' : 'html',
                'page' : 1
            },
            'onComplete' : function(responseTree, responseElements, responseHTML, responseJavaScript) {

                if(responseHTML){
                    // hide loading icon
                    if($('notifications_loading')) $('notifications_loading').setStyle('display', 'none');

                    $('notifications_menu').innerHTML = responseHTML;
                    $('notifications_menu').addEvent('click', function(event){
                        event.stop(); //Prevents the browser from following the link.

                        var current_link = event.target;
                        var notification_li = $(current_link).getParent('li');

                        // if this is true, then the user clicked on the li element itself
                        if (notification_li.id == 'core_menu_mini_menu_update') notification_li = current_link;

                        var forward_link;
                        if(current_link.get('href')){
                            forward_link = current_link.get('href');
                        }
                        else{
                            forward_link = $(current_link).getElements('a:last-child').get('href');
                        }

                        if(notification_li.get('class')=='notifications_unread'){
                            notification_li.removeClass('notifications_unread');
                            en4.core.request.send(new Request.JSON({
                                url : en4.core.baseUrl + 'activity/notifications/markread',
                                data : {
                                    format     : 'json',
                                    'actionid' : notification_li.get('value')
                                },
                                onSuccess : window.location = forward_link
                            }));
                        }
                        else window.location = forward_link;
                    });
                }
                else $('notifications_loading').innerHTML = '<?php echo $this->string()->escapeJavascript($this->translate("You have no new updates."));?>';
            }
        }).send();
    };


</script>
<script type='text/javascript'>
    en4.core.runonce.add(function(){
        new OverText($('global_search_field'), {
            poll: true,
            pollInterval: 500
        });
    });

</script>

<script type='text/javascript'>

    function switchto(q){
        if (q){
            document.getElementById('passwordtext').style.display="none";
            document.getElementById('password').style.display="inline";
            document.getElementById('password').focus();            
        } else {
            document.getElementById('password').style.display="none";
            document.getElementById('passwordtext').style.display="inline";              		          
        }
    }

    function myKeyPressed(e){
        e = e || window.event;
        var unicode=e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (unicode == 13){
            document.frmlogin.submit();
        }
    }
</script>
<script type="text/javascript">
    function Over() {

        document.getElementById('account_menu_mini').className = 'account_mini_activee';
    }
    function Out() {

        document.getElementById('account_menu_mini').className = 'view_more_account_mini';
    }
</script>
